package com.gexin.controller.xapp.index;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.interceptor.XAppSafeInterceptor;
import com.gexin.core.web.AppController;
import com.gexin.model.notice.NoticeNews;
import com.gexin.model.notice.NoticePlacard;
import com.gexin.model.system.AddrAreas;
import com.gexin.model.system.AddrCities;
import com.gexin.model.system.AddrProvinces;
import com.gexin.model.weixin.WeixinUser;
import com.gexin.service.index.XappIndexService;
import com.gexin.service.notice.NoticeNewsService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
import java.util.Map;


/**
 * 小程序首页接口
 */
@Before(XAppSafeInterceptor.class)//统一错误拦截
@ControllerBind(controllerKey = "/xapp/index")
public class IndexController extends AppController {

	/**
	 * 商品首页
	 */
	@Clear
	public void index(){
//		展示banner、扫码、产品分类、产品首页推荐、预售产品首页推荐、资讯首页推荐、平台公告首页推荐等。
		Map<String, Object> index = XappIndexService.dao.findIndex();
		renderJson(new ApiResponse(true,"success",index));
	}

	/**
	 *	公告列表
	 */
	@Clear
	public void noticeNewsList(){
		int pageSize = getPageSize();
		int pageIndex = getPageIndex();
		NoticePlacard noticeNews = new NoticePlacard();
		noticeNews.setState(0);
		Page<NoticePlacard> query = NoticePlacard.dao.query(pageIndex , pageSize , noticeNews);
		success(query);
	}

	/**
	 *	公告详情
	 */
	@Clear
	public void noticeNewInfo(){
		Integer noticeId = getParaToInt();
		if(noticeId==null){
			fail("id不能为null!");
			return;
		}
		NoticePlacard placard = NoticePlacard.dao.findById(noticeId);
		success(placard);
	}

	/**
	 * 资讯列表
	 */
	@Clear
	public void noticeList(){
		int pageIndex = getPageIndex();
		int pageSize = getPageSize();
		NoticeNews noticeNews = new NoticeNews();
		noticeNews.setState(0);
		Page<NoticeNews> newsPage = NoticeNews.dao.query(pageIndex , pageSize , noticeNews);
		success(newsPage);
	}

	/**
	 * 资讯详情
	 */
	@Clear
	public void noticeInfo(){
 		Integer noticeId = getParaToInt();
		if(noticeId==null){
			fail("id不能为null!");
			return;
		}
		WeixinUser authUser = getAuthUser();
		NoticeNews notice = NoticeNewsService.dao.findInfo(noticeId,authUser);
		success(notice);
	}

	/**
	 * 评论咨询
	 */
	public void noticeComment(){
		Integer noticeId = getParaToInt("notice_id");
		if(noticeId==null){
			fail("资讯不能为空!");
			return;
		}
		String content = getPara("content");
		if(StringUtil.isBlank(content)){
			fail("评论内容不能为空!");
			return;
		}
		content=content.trim();
		if(StringUtil.isBlank(content)){
			fail("评论内容不能为空!");
			return;
		}
		Integer commentId = getParaToInt("comment_id");
		WeixinUser authUser = getAuthUser();
		ApiResponse apiResponse = NoticeNewsService.dao.commentNotice(noticeId , content , commentId , authUser);
		renderJson(apiResponse);
	}


	/**
	 * 获取所有省市县
	 * /xapp/index/findAllAddr
	 */
	public void findAllAddr(){
		List<AddrProvinces> addrProvinces = null;
		if(addrProvinces==null){
			addrProvinces = AddrProvinces.dao.findAll();
			for (AddrProvinces addrProvince : addrProvinces) {
				List<AddrCities> addrCities = AddrCities.dao.getCitiesByProvinceId(addrProvince.getProvinceid());
				for (AddrCities addrCity : addrCities) {
					List<AddrAreas> areas = AddrAreas.dao.getAreasByCityId(addrCity.getCityid());
					addrCity.put("areas",areas);
				}
				addrProvince.put("citys",addrCities);
			}
//			CacheUtils.set("allCityList",addrProvinces);
		}
		renderJson(addrProvinces);
	}

}
